home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14649 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.3 KB  |  139 lines

  1. Path: news-1.csn.net!ub!newserve!rebecca!rpi!not-for-mail
  2. From: Christopher Kline <ckline@tc.cornell.edu>
  3. Newsgroups: comp.lang.c++.moderated,comp.lang.c++
  4. Subject: Problem with the relationship between friend and derived classes
  5. Date: 31 Mar 1996 17:21:37 -0000
  6. Organization: Cornell University
  7. Sender: cppmods@netlab.cs.rpi.edu
  8. Approved: Dietmar.Kuehl@uni-konstanz.de
  9. Message-ID: <4jmev1$c0r@netlab.cs.rpi.edu>
  10. NNTP-Posting-Host: netlab.cs.rpi.edu
  11. X-Original-Date: Sat, 30 Mar 1996 20:24:50 -0500
  12.  
  13. I have a problem involving the relationship between friend classes and
  14. derived classes. 
  15.  
  16. My group is currently writing a simulation framework involving moving
  17. vehicles, each of which have an associated set of dynamics which
  18. control their movement and their interaction with other vehicles in
  19. the simulation. 
  20.  
  21. The problem I am having can be seen in this minimal example (the
  22. errors are included as comments at point where they occur):
  23.  
  24.  
  25. class engine { 
  26.  
  27. public:
  28.   engine(car *c) {
  29.     attachedCar = c;
  30.   };
  31.   
  32.   virtual void update(void) = 0;  
  33.  
  34. protected:
  35.   car *attachedCar;  
  36. };
  37.  
  38. //--------------------
  39.  
  40. class car {
  41.  
  42.   friend engine;
  43.   
  44. public:
  45.   car(void) {
  46.     velocity = 0;
  47.   }
  48.  
  49.   virtual void move(void) = 0;
  50.  
  51. protected:
  52.   engine *e;
  53.   double velocity;
  54.   
  55. };
  56.  
  57. // -------------------
  58.  
  59. class honda_engine : public engine {
  60.  
  61. public:
  62.   honda_engine(honda *h)
  63.     : engine(h) { }
  64.   
  65.   void update(void) {
  66.  
  67.     // Error #1: "engine::attachedCar" is inaccessible   
  68.     // Error #2: "car::velocity" is inaccessible
  69.     attachedCar->velocity++; 
  70.                  
  71.     // Error #3: "honda_miles" is undefined
  72.     honda_miles++;         
  73.   }
  74.  
  75. };
  76.  
  77. //--------------------
  78.  
  79. class honda : public car {
  80.  
  81.   friend honda_engine;
  82.  
  83. public:
  84.   honda(void) {
  85.     honda_miles = 0;
  86.     e = new honda_engine(this);
  87.   }
  88.  
  89.   void move(void) {
  90.     e->update();
  91.   }
  92.  
  93. private:
  94.   int honda_miles;
  95.   
  96. };
  97.  
  98. //--------------------
  99.  
  100.  
  101. Engines are friends of their respective cars and the base classes for
  102. engines and cars are both abstract classes (hence only instances of
  103. the derived classes will be created). 
  104.  
  105. The problem is that I would like:
  106.  
  107. 1) a derived engine to have access to the protected members of the
  108. derived car class of which it is a friend, and of the parent class(es)
  109. of that car.
  110.  
  111. 2) a derived engine to access the correct class of car when it
  112. dereferences the attachedCar member (attachedCar will always be
  113. something derived from car, even if the compiler can't understand
  114. that).
  115.  
  116.  
  117. I understand WHY these three errors are occurring, but I can't figure
  118. out HOW to get my code to behave the way I want it to (i.e., using the
  119. relationship between cars/engines that I've set up).
  120.  
  121. Does anyone have any clue on how to get this to work? It seems like it
  122. would be a fairly common design problem, but I haven't found reference
  123. to it in any of my books or the FAQ.
  124.  
  125. Any help would be greatly appreciated. Also, if you would kindly CC:
  126. me on the reply, that would be great, in case I miss the post in the
  127. newsgroup.
  128.  
  129. -- 
  130. Christopher Kline 
  131. http://www.tc.cornell.edu/~ckline/
  132.  
  133. to compose, Decompose, I wander and slip
  134.  
  135.       [ Articles to moderate: mailto:c++-submit@netlab.cs.rpi.edu ]
  136.       [  Read the C++ FAQ: http://www.connobj.com/cpp/cppfaq.htm  ]
  137.       [  Moderation policy: http://www.connobj.com/cpp/guide.htm  ]
  138.       [      Comments? mailto:c++-request@netlab.cs.rpi.edu       ]
  139.